export default {
  //设置cookie
  setCookie(key, value, time) {
    // value加密
    let newValue = ''
    if (value) {
      value = JSON.stringify(value);
      for (var i = 0; i < value.length; i++) {
        newValue += String.fromCharCode(value.charCodeAt(i) - 100);
      }
    }
    if (time) {
      var date = new Date();
      date.setDate(date.getDate() + time);
      document.cookie = key + '=' + escape(newValue) + ';expires=' + date + ";path=/";
    } else {
      document.cookie = key + '=' + escape(newValue);
    }
  },
  //获取cookie
  getCookie(key) {
    var str = document.cookie;
    var arr = str.split('; ');
    var val = '';
    var oldValue = '';
    for (var i = 0; i < arr.length; i++) {
      var arr2 = arr[i].split('=');
      if (arr2[0] == key) {
        val = unescape(arr2[1]);
        break;
      }
    }
    // val解密
    if (val) {
      for (var i = 0; i < val.length; i++) {
        oldValue += String.fromCharCode(val.charCodeAt(i) + 100);
      }
    }
    if (oldValue) {
      return JSON.parse(oldValue);
    } else {
      return false;
    }
  },

  //删除cookie
  removeCookie(key) {
    this.setCookie(key, '', -1);
  },

  //时间戳转年月日
  getTime(time, type) {
    var date;
    if (type) {
      date = new Date(time);
    } else {
      date = new Date(time * 1000);
    }
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    return {
      date,
      year,
      month,
      day,
      ymd: year + '-' + this.setToTime(month) + '-' + this.setToTime(day),
      ymdhms: year + '-' + this.setToTime(month) + '-' + this.setToTime(day) + ' ' + this.setToTime(hour) + ':' + this.setToTime(min) + ':' + this.setToTime(sec),
      ymdhm: year + '-' + this.setToTime(month) + '-' + this.setToTime(day) + ' ' + this.setToTime(hour) + ':' + this.setToTime(min),
      mdhms: this.setToTime(month) + '-' + this.setToTime(day) + ' ' + this.setToTime(hour) + ':' + this.setToTime(min) + ':' + this.setToTime(sec),
      mdhm: this.setToTime(month) + '-' + this.setToTime(day) + ' ' + this.setToTime(hour) + ':' + this.setToTime(min),
      hs: this.setToTime(hour) + ':' + this.setToTime(min)
    }
  },

  //时间补零函数
  setToTime(num) {
    return num < 10 ? '0' + num : num;
  },
  // 动态设置title
  setTitle(title) {
    document.title = title;
  },

  /*
	阿拉伯数字转中文数字
	参数：
		str:阿拉伯数字   num || str
    */
  toChineseNum(str) {
    str = ('' + str).trim().replace(/^0*/, ''); //去掉前面修饰的0
    var match = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '零'];
    return ('0000' + str).substr((str.length % 4) || 4).replace(/(\d){4}/g, function (_str, endIndex, startIndex) {
      var dot = (((str.length - 1) / 4) >> 0) - ((startIndex / 4) >> 0);
      var prefix = (function getPrfix(dot) {
        return (dot > 2 ? (+_str ? (dot == 3 ? '万' : (getPrfix(dot - 1) + '万')) : '') : (dot == 1 ? (+_str ? '万' : '') : (dot == 2 ? '亿' : '')));
      })(dot);
      /0+$/g.test(_str) && (prefix += match[10]); //处理单元内后半部分有零的地方
      return (+_str) ? (_str.replace(/(\d)(\d)(\d)(\d)/g, function ($0, $1, $2, $3, $4) {
        !match[$1] && (match[$2] ? ($1 = 10) : (match[$3] ? ($2 = 10) : (match[$4] ? ($3 = 10) : ''))); //处理相邻单元前半部分
        match[$1] && match[$3] && !match[$2] && ($2 = 10), match[$2] && match[$4] && !match[$3] && ($3 = 10), match[$1] && match[$4] && !match[$3] && !match[$2] && ($3 = 10); //中间两个连续为0，只是获取最后一个
        return (match[$1] && ($1 < 10 ? (match[$1] + '千') : match[$1])) + (match[$2] && ($2 < 10 ? (match[$2] + '百') : match[$2])) + (match[$3] && ($3 < 10 ? ($3 == 1 ? '十' : (match[$3] + '十')) : match[$3])) + (match[$4] && match[$4]);
      }) + prefix) : (prefix);
    }).replace(/^零*/g, '').replace(/零*$/g, '').replace(/(零)*/g, '$1').replace(/零亿/g, '亿') || match[10]; //处理连续零的问题
  },

  // 深拷贝
  deepClone(obj) {
    let newObj = obj.push ? [] : {};
    for (var key in obj) {
      if (obj[key] instanceof Object) {
        newObj[key] = this.deepClone(obj[key])
      } else {
        newObj[key] = obj[key]
      }
    }
    return newObj;
  },

  // 验证是否登录
  verificationLogIn() {
    if (sessionStorage.getItem('identifying') > 0) {
      return true;
    }
    return false;
  },

  // 验证是否弹出礼包
  verificationGift() {
    if (sessionStorage.getItem('gift')) {
      return true;
    }
    return false;
  },

  // 函数节流，处理高频触发事件性能问题
  /* 
      func：Function   触发的函数
      wait: Number     触发间隔
  */
  throttle(func, wait) {
    var ctx, args, rtn, timeoutID;
    var last = 0;

    return function throttled() {
      ctx = this;
      args = arguments;
      var delta = new Date() - last;
      if (!timeoutID) {
        if (delta >= wait) {
          call();
        } else {
          timeoutID = setTimeout(call, wait - delta);
        }
      }
    };

    function call() {
      timeoutID = 0;
      last = +new Date();
      rtn = func.apply(ctx, args);
      ctx = null;
      args = null;
    }
  },
  //时间差转时分秒
  diffToHSM(diff) {
    var hour = Math.floor(diff / 1000 / 60 / 60);
    var min = Math.floor(diff / 1000 / 60 % 60);
    var sec = Math.floor(diff / 1000 % 60);
    return {
      hour: this.setToTime(hour),
      min: this.setToTime(min),
      sec: this.setToTime(sec)
    }
  },
  // 判断是否移动端
  isMobile() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      return true;
    }
  }
}
